package com.qf.co.controller.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 * 请求访问会第一给进入这个过滤器，过滤器处理完成过后才会执行Srevlet
 * 字符集乱码处理器
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.filter
 * @Description:
 * @Date 2022/5/21 20:58
 */
//todo 过滤所有
@WebFilter("/*")
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
      //处理请求字符集乱码
        servletRequest.setCharacterEncoding("utf-8");
        //处理响应字符集乱码
        servletResponse.setCharacterEncoding("utf-8");
        servletResponse.setContentType("text/html;charset=utf-8");
        //放行请求到下一个过滤器
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
